import request from '@/config/axios'

export interface CourseMaterialDO {
  id: number
  name: string
  fileType: number
  materialId: number
  fileSize: number
  fileTime: number
  createTime: string
}

export interface CourseMaterialClassDO {
  id: number
  type: number
  name: string
  parentId: number
  children?: CourseMaterialClassDO[]
}

export interface MaterialVO {
  id: number // id
  name: string // 文件名称
  fileType: number // 文件类型
  materialId: number // 文件目录
  fileSize: number // 文件大小
  fileTime: number // 课件时长
  status: number // 状态
}

export const getMaterialClassList = async (type): Promise<CourseMaterialClassDO[]> => {
  return await request.get({ url: '/material/course/list?type=' + type })
}

export const addAndUpdateChild = async (
  data: CourseMaterialClassDO
): Promise<CourseMaterialClassDO> => {
  return await request.post({ url: '/material/course/addAndUpdateChild', data: data })
}

export const uploadFile = async (data: FormData) => {
  return await request.post({
    url: '/material/course/uploadFile',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 删除部门
export const deleteChild = async (ids: number | number[]) => {
  const data = Array.isArray(ids) ? ids : [ids]
  return await request.post({
    url: '/material/course/deleteChild',
    data: { materialId: data }
  })
}

// 查询课程素材管理分页
export const getMaterialPage = async (params: any) => {
  return await request.get({ url: `/material/course/page`, params })
}

// 查询素材数量
export const getFileTypeCount = async (params: any) => {
  return await request.get({ url: `/material/course/getFileTypeCount`, params })
}

// 删除素材
export const deleteMaterial = async (id: number) => {
  return await request.delete({ url: '/material/course/deleteMaterial?id=' + id })
}

// 获取单个文件信息
export const getMaterial = (id: number) => {
  return request.get({
    url: `/material/course/get/${id}`
  })
}

// 更新文件信息
export const updateMaterial = (data: Partial<CourseMaterialDO>) => {
  return request.put({
    url: '/material/course/update',
    data
  })
}
